package com.qiaoanju.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.qiaoanju.domain.UserDomain;
import com.qiaoanju.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/simple/{id}")
    public UserDomain findById(@PathVariable Long id) {
        return this.userService.findOne(id);
    }


    @PostMapping("/user")
    public UserDomain postUser(@RequestBody UserDomain user) {
        return this.userService.postUser(user);
    }
    @GetMapping("/checkUser")
    public UserDomain checkUser(@RequestParam Map<String,Object> map){
        return this.userService.checkUser(map);
    }

    @Autowired
    private EurekaClient eurekaClient;

    @Autowired
    private DiscoveryClient discoveryClient;

    //获取服务提供的ip
    @GetMapping("/eureka-instance")
    public String serviceUrl() {
        InstanceInfo instance = this.eurekaClient.getNextServerFromEureka("QIAOANJU-MICROSOFTS-EUREKA-CLIENT-PROVIDER", false);
        return instance.getHomePageUrl();
    }

    //获取服务提供者信息
    @GetMapping("/instance-info")
    public List<String> showInfo() {
        List<String> localServiceInstance = this.discoveryClient.getServices();
        return localServiceInstance;
    }
}
